using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using GonzalesCore.CoreElements;
using GonzalesCore.CoreManagers;
using Kypris2.CoreElements;
using Kypris2.Utils;
using Jamila2.Tools;

public partial class GonzalesToolsAllExpenseType : System.Web.UI.Page
{
    protected PrivateLoginSession CurrentPrivateSession = null;
    protected LogManager CurLogManager = null;
    protected readonly int TotalColumns = 4;
    protected DataObj CurPageDataObj = new DataObj();

    #region CurrentPageIndex
    protected int CurrentPageIndex
    {
        get
        {
            return (int)ViewState["CurrentPageIndex"];
        }

        set
        {
            ViewState["CurrentPageIndex"] = value;
        }
    }
    #endregion

    #region ResultTable
    protected DataTable ResultTable
    {
        get
        {
            return (DataTable)ViewState[GonzalesCommonNamingConvention.CurrentPrivateSessionViewState];
        }

        set
        {
            ViewState[GonzalesCommonNamingConvention.CurrentPrivateSessionViewState] = value;
        }
    }
    #endregion

    #region GridViewHeader
    protected ArrayList GridViewHeader
    {
        get
        {
            return (ArrayList)ViewState["GridViewHeader"];
        }

        set
        {
            ViewState["GridViewHeader"] = value;
        }
    }
    #endregion

    #region SelectLink
    protected string SelectLink
    {
        get
        {
            //return ViewState["MandatoryMessage"].ToString();
            string text = (string)ViewState["SelectLink"];
            if (text != null)
                return text;
            else
                return string.Empty;
        }

        set
        {
            ViewState["SelectLink"] = value;
        }
    }
    #endregion

    #region Events
    protected void Page_Load(object sender, EventArgs e)
    {
        this.SessionCheck();

        if (!IsPostBack)
        {
            
            this.CurrentPageIndex = 0;

            this.PageComponentSetup();
            this.SwitchLanguage();

            this.LoadData();
            this.DropDownList_SortBy_SelectedIndexChanged(sender, e);
        }
    }
    protected void DropDownList_SortBy_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (this.ResultTable != null)
        {
            DataView ResultView = new DataView(this.ResultTable);
            ResultView.Sort = this.DropDownList_SortBy.SelectedValue + " " + this.DropDownList_SortOrder.SelectedValue;
            this.BindData(ResultView, this.CurrentPageIndex);
           
        }
    }
    protected void DropDownList_SortOrder_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.DropDownList_SortBy_SelectedIndexChanged(sender, e);
    }
    protected void GridViewResult_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.CurrentPageIndex = e.NewPageIndex;
        this.DropDownList_SortBy_SelectedIndexChanged(sender, e);
    }

    #region OnRowCreated
    protected void OnRowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            TableCellCollection cells = e.Row.Cells;

            for (int i = 0; i < this.GridViewHeader.Count; i++)
            {
                cells[i].Text = this.GridViewHeader[i].ToString();
            }
        }
    }
    #endregion

    #endregion

    #region Customized Methods

    #region SessionCheck
    public void SessionCheck()
    {

        if (Session[GonzalesCommonNamingConvention.CurrentPrivateSession] != null)
        {
            this.CurrentPrivateSession = (PrivateLoginSession)Session[GonzalesCommonNamingConvention.CurrentPrivateSession];
            this.CurLogManager = new LogManager(this.CurrentPrivateSession);    // activate Log Manager
        }
        else
        {
            Response.Redirect(GonzalesErrorPageRedirect.SessionError);
        }

    }
    #endregion

    #region PageComponentSetup
    protected void PageComponentSetup()
    {
        this.Button_Cancel.Attributes["onclick"] = "if(CloseWindow()){parent.ModalWindow.hide();} return false;";
    }
    #endregion

    #region SwitchLanguage
    protected void SwitchLanguage()
    {
        try
        {
            using (MultiLingualFactory CurLanguageFactory = MultiLingualEngine.GetMultiLingualFactory(this, this.CurrentPrivateSession.CurrentLoggedOnUser.PreferedLanguage.InternalID))
            {
                this.Label_Title.Text = CurLanguageFactory.DisplayValue("Title");
                this.Button_Cancel.Text = CurLanguageFactory.DisplayValue("Cancel");

                this.GridViewHeader = new ArrayList();
                this.GridViewHeader.Add(CurLanguageFactory.DisplayValue("Code"));
                this.GridViewHeader.Add(CurLanguageFactory.DisplayValue("Category"));
                this.GridViewHeader.Add(CurLanguageFactory.DisplayValue("DisplayName"));
                this.SelectLink = CurLanguageFactory.DisplayValue("SelectLink");

                this.DropDownList_SortBy.Items.Add(new ListItem(CurLanguageFactory.DisplayValue("Code"), this.CurPageDataObj.Code));
                this.DropDownList_SortBy.Items.Add(new ListItem(CurLanguageFactory.DisplayValue("DisplayName"), this.CurPageDataObj.DisplayName));

                this.DropDownList_SortOrder.Items.Add (new ListItem(CurLanguageFactory.DisplayValue("ASC"), "ASC"));
                this.DropDownList_SortOrder.Items.Add (new ListItem(CurLanguageFactory.DisplayValue("DESC"), "DESC"));
            }
        }
        catch (Exception ex)
        {
            this.CurLogManager.LogError("[GonzalesMyClaims.SwitchLanguage.EX] : " + ex.Message, this.CurrentPrivateSession.CurrentLoggedOnUser, LogInterface.User);
        }

    }
    #endregion

    #endregion

    #region Data Operation

    #region LoadData
    protected void LoadData()
    {
        using (GLClaimTypeManager CurClaimTypeManager = new GLClaimTypeManager(this.CurrentPrivateSession))
        {

            MasterBaseCollection ResultObj = CurClaimTypeManager.GetAllClaimTypeInLang(this.CurrentPrivateSession.CurrentLoggedOnUser.PreferedLanguage);

            if (ResultObj != null)
            {
                this.ResultTable = new DataTable();
                ResultTable.Columns.Add(new DataColumn(this.CurPageDataObj.InternalID, Type.GetType("System.String")));
                ResultTable.Columns.Add(new DataColumn(this.CurPageDataObj.Code, Type.GetType("System.String")));
                ResultTable.Columns.Add(new DataColumn(this.CurPageDataObj.DisplayName, Type.GetType("System.String")));
                ResultTable.Columns.Add(new DataColumn(this.CurPageDataObj.Category, Type.GetType("System.String")));
                ResultTable.Columns.Add(new DataColumn(this.CurPageDataObj.SelectLink, Type.GetType("System.String")));

                DataRow RowObj = null;

                for (int x = 0; x < ResultObj.Count; x++)
                {
                    GLClaimType CurGLClaimType = (GLClaimType)ResultObj[x];

                    RowObj = ResultTable.NewRow();

                    RowObj[this.CurPageDataObj.InternalID] = CurGLClaimType.InternalID;
                    RowObj[this.CurPageDataObj.Code] = CurGLClaimType.GLCode;
                    RowObj[this.CurPageDataObj.DisplayName] = CurGLClaimType.DisplayName;
                    RowObj[this.CurPageDataObj.Category] = CurGLClaimType.Category;
                    RowObj[this.CurPageDataObj.SelectLink] = "<a href='#' onClick=\"SelectOnClick('" + CurGLClaimType.InternalID + "', '" + CurGLClaimType.DisplayName + "', '" + CurGLClaimType.GLCode + "');\" class='GridViewLink'>" + this.SelectLink + "</a>";

                    ResultTable.Rows.Add(RowObj);
                }
            }
            else
            {
                this.CurLogManager.LogError("[GonzalesAdminExpenseCompanyListing.LoadData] : Failed at CurClaimTypeManager.GetActiveClaimTypeInLangByEntity] : " + CurClaimTypeManager.ErrorMessage, this.CurrentPrivateSession.CurrentLoggedOnUser, LogInterface.System);
            }

        }
    }
    #endregion
    
   
    private void BindData(DataView result)
    {
        this.GridViewResult.DataSource = result;
        this.GridViewResult.DataBind();
    }

    private void BindData(DataView result, int pageindex)
    {
        this.GridViewResult.PageIndex = pageindex;
        this.GridViewResult.DataSource = result;
        this.GridViewResult.DataBind();
    }

    #endregion

    #region Class: DataObj

    protected class DataObj
    {
        public readonly string InternalID = "InternalID";
        public readonly string Category = "Category";
        public readonly string Code = "Code";
        public readonly string DisplayName = "DisplayName";
        public readonly string SelectLink = "SelectLink";   
    }

    #endregion

}
